var tree;
var layer;
$(function () {
    layer = layui.layer;
    var oTable = new TableInit();
    oTable.Init();
    $("#expandAllTree").on('click', function () {
        if ($("#expandAllTree").val() == '展开所有') {
            $('#tree_table').bootstrapTable("expandAllTree")//展开\
            $("#expandAllTree").val('折叠所有')
        } else {
            $('#tree_table').bootstrapTable("collapseAllTree")//折叠
            $("#expandAllTree").val('展开所有')
        }
    })

});


var TableInit = function () {

    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $.ajax({
            url: '/fault_list',
            method: 'get',
            dataType: 'json',
            async: false,
            success: function (data) {
                tree = JSON.stringify(data)
            }
        })
        $('#tree_table').bootstrapTable({
            class: 'table table-hover table-bordered',
            data: eval(tree),
            pagination: false,
            treeView: true,
            striped: true,   //是否各行渐变色
            bordered: true,  //是否显示边框
            treeId: "id",
            treeField: "events",
            showExport: true,                     //是否显示导出
            exportTypes: ['txt', 'doc', 'excel',],  //导出文件类型
            exportDataType: "all",              //basic', 'all', 'selected'.
            columns: [{
                align: 'center',
                title: '编号',
                width: 5,
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'id',
                visible: false,
            }, {
                align: 'left',
                field: 'events',
                title: '事件',
                editable: {
                    type: 'text',
                    validate: function (data, value, row) {
                        if ($.trim(data) == '') return '请输入事件';
                    }
                }
            }, {
                align: 'center',
                field: 'type',
                title: '事件类型',
                width: 10,
            }, {
                align: 'center',
                field: 'describe',
                title: '描述',
                editable: {
                    type: 'text',
                    validate: function (data) {
                        if ($.trim(data) == '') return '请输入描述';
                    }
                }
            }, {
                align: 'center',
                title: '操作',
                formatter: function (value, row) {
                    a = '<button class="layui-btn layui-btn-primary layui-btn-sm"  data-toggle="modal" data-target="#addeventModal"' +
                        ' onclick="update_top(' + row.id + ')" ><i class="layui-icon">&#xe654;</i></button>　'
                    if (row.type == 'X') {
                        a = ""
                    }
                    b = '<button class="layui-btn layui-btn-primary layui-btn-sm"   data-toggle="modal" data-target="#delModal"' +
                        ' onclick="delete_eve(' + row.id + ')"' +
                        ' > <i class="layui-icon">&#xe640;</i></button>　'
                    return '<div class="layui-btn-group">' + a + b
                    '</div>'
                }
            },
            ],
            onEditableSave: function (field, row, oldValue, $el) {
                $.ajax({
                    type: "POST",
                    url: "/fault_upd/",
                    data: row,
                    success: function (data) {
                        if (data == "ok") {
                            layer.msg('修改成功', {icon: 6});
                        } else {
                            layer.msg('修改失败', {icon: 5});
                        }
                    },
                    error: function () {
                        layer.msg('后台error', {icon: 5});
                    },
                });
            }
        });
    }
    return oTableInit;
};


$(function () {
    // 添加顶事件
    $('#Tevents').blur(function () {
        $('#evspan').removeClass()
        if ($.trim($('#Tevents').val()) == '') {
            $('#evspan').addClass('show')
            return false
        }
    })
    $('#Tdescribe').blur(function () {
        $('#desspan').removeClass()
        if ($.trim($('#Tdescribe').val()) == '') {
            $('#desspan').addClass('show')
            return false
        }
    })
    $('#addTBtn').click(function () {
        $.ajax({
            url: '/fault_addt/',
            method: 'get',
            data: 'tevents=' + $.trim($('#Tevents').val()) + '&describe=' + $.trim($('#Tdescribe').val()),
            beforeSend: function () {
                if ($.trim($('#Tevents').val()) == '') {
                    $('#evspan').removeClass()
                    $('#evspan').addClass('show')
                    return false
                }
                if ($.trim($('#Tdescribe').val()) == '') {
                    $('#desspan').removeClass()
                    $('#desspan').addClass('show')
                    return false
                }
                $('#addTModal').modal('hide')
            },
            success: function (data) {
                if (data == 'ok') {
                    layer.msg('添加成功', {icon: 6})
                    setTimeout('location.reload()', 2000)
                } else {
                    layer.msg('添加失败', {icon: 5})
                }
            },
            error: function () {
                layer.msg('后台有问题！！!', {icon: 5})
            }
        })
    })
    // 删除事件
    $('#deleventBtn').click(function () {
        $('#delModal').modal('hide')
        $.get('/fault_del', {'id': $('#delevent').val()}, function (data) {
            if (data == 'ok') {
                layer.msg('删除成功', {icon: 6})
                setTimeout('location.reload()', 2000)
            } else {
                layer.msg('删除失败', {icon: 5})
            }
        })

    })
//添加事件

    $('#MXevents').blur(function () {
        $('#addevspan').removeClass()
        if ($.trim($('#MXevents').val()) == '') {
            $('#addevspan').addClass('show')
            return false
        }
    });
    $('#adddescribe').blur(function () {
        $('#addesspan').removeClass()
        if ($.trim($('#adddescribe').val()) == '') {
            $('#addesspan').addClass('show')
            return false
        }
    })
    $('#addevenBtn').click(function () {
        $.ajax({
            url: '/fault_addmx/',
            data: {
                'event': $.trim($('#MXevents').val()),
                'describe': $.trim($('#adddescribe').val()),
                'id': $.trim($('#addeve').val()),
                'type': $("input[name='optionsRadiosinline']:checked").val(),
            },
            method: 'post',
            beforeSend: function () {
                if ($.trim($('#MXevents').val()) == '') {
                    $('#addevspan').removeClass()
                    $('#addevspan').addClass('show')
                    return false
                }
                if ($.trim($('#adddescribe').val()) == '') {
                    $('#addesspan').removeClass()
                    $('#addesspan').addClass('show')
                    return false
                }
                $("#addeventModal").modal('hide')
            },
            success: function (data) {
                if (data == 'ok') {
                    layer.msg('添加成功', {icon: 6})
                    setTimeout('location.reload()', 2000)
                } else {
                    layer.msg('添加失败', {icon: 5})
                }
            }, error: function () {
                layer.msg('后台有问题！！!')
            }
        })

    })

});


function update_top(id) {
    $('#addeve').val(id)
};

function delete_eve(id) {
    $('#delevent').val(id)
}